Record Locking

Description

Alpha Anywhere places a lock on a record when:

  • a user starts changing a record, or entering a new record

  • the user commits the changes

There is no need for the end user to explicitly lock and unlock records. You can test if a record is locked by using <tbl>.IS_RECORD_LOCKED().

Open 2 the same table twice. Both t1 and t2 point to the same record.

t1 = table.open("customer") 
? t1.recno() 
= 1 
t2 = table.open("customer") 
? t2.recno() 
= 1

Note how the <tbl>.CHANGE_BEGIN() used by t1 method locks the record for t2.

? t1.is_record_locked() 
= .F. 
t1.change_begin()
  
? t2.is_record_locked() 
= .T.

When t2 uses <tbl>.CHANGE_BEGIN() an error results because the record is locked by t1.

t2.change_begin() 
ERROR: The process cannot access the file because another process has locked a portion of the file. 
 customer.DBF

The use of <tbl>.CHANGE_END() by t1 releases the lock.

t1.change_end() 
? t2.is_record_locked() 
= .F.

Now t2 can go into change mode without causing an error.

t2.change_begin() 
t2.change_end()

Example

Change the value in the COUNTRY field. If already in Change mode then do not change record.

'Create an object pointer to the current table
tbl = table.current()
if tbl.mode_get()> 0 then
    'Compute the Message Type code
    code = UI_ATTENTION_SYMBOL
    ui_msg_box("Warning", "Already in data entry mode.", code)
else
    commit_flag = .T.
    tbl.change_begin()
    on error goto error_handler
    tbl.last_name = "Washington"
    tbl. first_name = "George"
    tbl.change_end(commit_flag)
end if
end
error_handler:
   commit_flag = .F.
RESUME NEXT

Same as above example, but uses the newer <tbl>.IS_RECORD_LOCKED() method (introduced in V5), instead of ON ERROR.

'Create an object pointer to the current table
tbl = table.current()
if tbl.mode_get()> 0 then
    code = UI_ATTENTION_SYMBOL
    ui_msg_box("Warning", "Already in data entry mode.", code)
else
    if (tbl.is_record_locked()= .f.) then
       tbl.change_begin()
       tbl.last_name = "Washington"
       tbl. first_name = "George"
       tbl.change_end(.T.)
    end if
end if
end

This next example contrasts the .CHANGE_BEGIN() method (which works with tables "behind the scenes") with the form methods for changing the record that is currently displayed on a form. This script could be on a button on a form. Note that, just as when you are editing data in a form, there is no need to explicitly put the form into change mode. as soon as you change the value property of the Last_name control, the record is automatically placed in change mode.

Last_name.value = "Washington"
First_name.value = "George"
parent.commit()

Limitations

Desktop applications only. Not available in Community Edition.